# # 13.1
# import cv2
# img = cv2.imread("shape1.jpg")
# gray =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# t,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
# contours,hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(img,contours,-1,(0,0,255),5)
# cv2.imshow("img",img)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
#
# # 13.2
# import cv2
# img=cv2.imread("flower.jpg")
# cv2.imshow("img",img)
# img=cv2.medianBlur(img,5)
# gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# t,binary=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
# cv2.imshow("binary",binary)
# contours,hierarchy=cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
# cv2.drawContours(img,contours,-1,(0,0,255),2)
# cv2.imshow("contours",img)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# # 13.3
# import cv2
# img=cv2.imread("shape2.jpg")
# gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# t,binary=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
# contours,hierarchy=cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# x,y,w,h=cv2.boundingRect(contours[0])
# cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
# cv2.imshow("img",img)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# # 13.4
# import cv2
# img=cv2.imread("shape2.jpg")
# gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# t,binary=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
# contours,hierarchy=cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# center,radius=cv2.minEnclosingCircle(contours[0])
# x=int(round(center[0]))
# y=int(round(center[1]))
# cv2.circle(img,(x,y),int(radius),(0,0,255),2)
# cv2.imshow("img",img)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# # 13.5
# import cv2
# img=cv2.imread("027.jpg")
# gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# ret,binary=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
# contours,hierarchy=cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# hull=cv2.convexHull(contours[0])
# cv2.polylines(img,[hull],True,(0,0,255),2)
# cv2.imshow("img",img)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# # 13.6
# import cv2
# img=cv2.imread("flower.jpg")
# r1=cv2.Canny(img,10,50);
# r2=cv2.Canny(img,100,200);
# r3=cv2.Canny(img,400,600);
# cv2.imshow("img",img)
# cv2.imshow("r1",r1)
# cv2.imshow("r2",r2)
# cv2.imshow("r3",r3)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
#
# # 13.7
# import cv2
# import numpy as np
# img = cv2.imread("bi3.jpg")
# o=img.copy()
# o=cv2.medianBlur(o,5)
# gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
# binary=cv2.Canny(o,50,150)
# lines=cv2.HoughLinesP(binary,1,np.pi/180,15,minLineLength=100,maxLineGap=18)
# for line in lines:
#     x1,y1,x2,y2=line[0]
#     cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
# cv2.imshow("canny",binary)
# cv2.imshow("img",img)
# cv2.waitKey()
# cv2.destroyAllWindows()
#
# # 13.8
# import cv2
# import numpy as np
# img=cv2.imread("coin.jpg")
# o=img.copy()
# o=cv2.medianBlur(o,5)
# gray=cv2.cvtColor(o,cv2.COLOR_BGR2GRAY)
# circles=cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1,70,param1=100,param2=25,minRadius=10,maxRadius=50)
# circles=np.uint(np.around(circles))
# for c in circles[0]:
#     x,y,r=c
#     cv2.circle(img,(x,y),r,(0,0,255),3)
#     cv2.circle(img,(x,y),2,(0,0,255),3)
# cv2.imshow("img",img)
# cv2.waitKey()
# cv2.destroyAllWindows()

# 14.1
import cv2

capture = cv2.VideoCapture(0)
while capture.isOpened():
    retval, image = capture.read()
    cv2.imshow("Video", image)
    key = cv2.waitKey(1)
    if key == 32:
        break
capture.release()
cv2.destroyAllWindows()

14.2
import cv2

capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while (capture.isOpened()):
    retval, image = capture.read()
    image_Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    if retval == True:
        cv2.imshow("Video", image)
        cv2.imshow("Video_Gray", image_Gray)
        key = cv2.waitKey(1)
        if key == 32:
            break
capture.release()
cv2.destroyAllWindows()

14.3
import cv2

cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
while(cap.isOpened()):
    ret,frame = cap.read()
    cv2.imshow("Video",frame)
    k = cv2.waitKey(1)
    if k == 32:
        cap.release()
        cv2.destroyWindow("Video")
        cv2.imwrite("D:/copy.png",frame)
        cv2.imshow('img',frame)
        cv2.waitKey()
        break
cv2.destroyAllWindows()

14.4
import cv2

cap_Inner = cv2.VideoCapture(0,cv2.CAP_DSHOW)
cap_Outer = cv2.VideoCapture(1,cv2.CAP_DSHOW)
while(cap_Inner.isOpened()&cap_Outer.isOpened()):
    retval,img_Inner = cap_Inner.read()
    ret,img_Outer = cap_Outer.read()
    cv2.imshow("Video_Inner",img_Inner)
    cv2.imshow("Video_Outer",img_Outer)
    key = cv2.waitKey(1)
    if key == 32:
        break
cap_Inner.release()
cap_Outer.release()
cv2.destroyAllWindows()

14.5
import cv2

video = cv2.VideoCapture("D:/32583/红旗.avi")
while(video.isOpened()):
    retval,image = video.read()
    cv2.namedWindow("Video",0)
    cv2.resizeWindow("Video",420,300)
    if retval == True:
        cv2.imshow("Video",image)
    else:
        break
    key = cv2.waitKey(1)
    if key == 27:
        break
video.release()
cv2.destroyAllWindows()

14.6
import cv2

video = cv2.VideoCapture("D:/32583/红旗.avi")
while(video.isOpened()):
    retval,img_Color = video.read()
    cv2.namedWindow("Gray",0)
    cv2.resizeWindow("Gray",420,300)
    if retval == True:
        img_Gray = cv2.cvtColor(img_Color,cv2.COLOR_BGR2GRAY)
        cv2.imshow("Gray",img_Gray)
    else:
        break
    key = cv2.waitKey(1)
    if key == 27:
        break
video.release()
cv2.destroyAllWindows()

14,7
import cv2

video = cv2.VideoCapture("D:/32583/红旗.avi")
while(video.isOpened()):
    retval,image = video.read()
    cv2.namedWindow("Video",0)
    cv2.resizeWindow("Video",420,300)
    if retval == True:
        cv2.imshow("Video",image)
    else:
        break
    key = cv2.waitKey(50)
    if key == 32:
        cv2.waitKey(0)
        continue
    if key == 27:
        break
video.release()
cv2.destroyAllWindows()

# 14.8

import cv2

video = cv2.VideoCapture("D:/32583/红旗.avi")
fps = video.get(cv2.CAP_PROP_FPS)
frame_Count = video.get(cv2.CAP_PROP_FRAME_COUNT)
frame_Width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_Height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("帧速率：",fps)
print("帧数：",frame_Count)
print("帧宽度：",frame_Width)
print("帧高度：",frame_Height)

# 14.9
import cv2

video = cv2.VideoCapture("D:/32583/红旗.avi")
fps = video.get(cv2.CAP_PROP_FPS)
frame_Num = 1
while (video.isOpened()):
    retval, frame = video.read()
    cv2.namedWindow("Video", 0)
    cv2.resizeWindow("Video", 420, 300)
    if retval == True:
        cv2.putText(frame, "frame:" + str(frame_Num), (0,100),
                    cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
        cv2.putText(frame, "second:" + str(round(frame_Num / fps, 2)) + "s",
                    (0, 200), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
        cv2.imshow("Video", frame)
    else:
        break
    key = cv2.waitKey(50)
    frame_Num += 1  #
    if key == 27:
        break
video.release()
cv2.destroyAllWindows()

#14.10
import cv2
import retval
from pandas.core import frame

capture = cv2.VideoCapture(0,cv2.CAP_DSHOW)
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
output = cv2.VideoWriter("output.avi",fourcc,20,(640,480))
while(capture.isOpened()):
    if retval == True:
        output.write(frame)
        cv2.imshow("frame",frame)
    key = cv2.waitKey(1)
    if key == 27:
        break
capture.release()
output.release()
cv2.destroyAllWindows()

# 14.11
import cv2

capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
fps = 20
output = cv2.VideoWriter("ten_Seconds.avi",fourcc,fps,(640,480))
frame_Num = 10 * fps
while(capture.isOpened() and frame_Num > 0):
    retval,frame = capture.read()
    if retval == True:
        output.write(frame)
        cv2.imshow("frame",frame)
    key = cv2.waitKey(1)
    frame_Num -= 1
capture.release()
output.release()
cv2.destroyAllWindows()

#14.12:
import cv2

video = cv2.VideoCapture("D:/32583/红旗.avi")
fps = video.get(cv2.CAP_PROP_FPS)
size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
output = cv2.VideoWriter("copy.avi",fourcc,fps,size)
while(video.isOpened()):
    retval,frame = video.read()
    if retval == True:
        output.write(frame)
    else:
        break
print("红旗.avi已经保存为PyCharm当前项目路径下的copy.avi")
video.release()
output.release()

# 14.13:
import cv2

video = cv2.VideoCapture("D:/32583/红旗.avi")
fps = video.get(cv2.CAP_PROP_FPS)
size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
output = cv2.VideoWriter("ten_Seconds.avi", fourcc, fps, size)
frame_Num = 10 * fps
while(video.isOpened() and frame_Num > 0):
    retval,frame = video.read()
    if retval == True:
        output.write(frame)
    frame_Num -= 1

print("红旗.avi的前10s视频已经保存为PyCharm当前路径下的ten_Seconds.avi")
video.release()
output.release()

print("Video")